package com.crm.service.repositoryService;

import com.crm.dao.repositoryDAO.InAndOutlogDAO;
import com.crm.dao.repositoryDAO.RepositoryDAO;
import com.crm.pojo.Repository;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class RepositoryService {
    @Autowired
    private RepositoryDAO repositoryDAO;
    @Autowired
    private InAndOutlogDAO inAndOutlogDAO;

    //添加一个仓库
    public void save(Repository repository){
        repository.setRepositoryIsDel("否");
        repository.setRaddress(repository.getProvince()+"-"+repository.getCity()+"-"+repository.getDistrict());
        repositoryDAO.save(repository);
    }
    //查询所有仓库数量
    public Long queryCount(){
        return repositoryDAO.queryCount();
    }
    //查询所有仓库 分页
    public List<Repository> allRepositoryPaged(int curpage,int pagesize){
        PageRequest pageRequest =new PageRequest(curpage-1,pagesize);
        return repositoryDAO.allRepositoryPaged(pageRequest);
    }
    //查询所有仓库 不分页
    public List<Repository> findAllR(){
        return repositoryDAO.allRepository();
    }
    //模糊查询仓库根据仓库名称和仓库地址
    public List<Repository> allRepositoryByMh(String q){
        return repositoryDAO.allRepositoryByMh("%"+q+"%");
    }
    //通过rid 查询一个仓库信息
    public Repository findOne(Long rid){
        Repository repository =  repositoryDAO.findOne(rid);
        String[] strings = repository.getRaddress().split("-");
        repository.setProvince(strings[0]);
        repository.setCity(strings[1]);
        repository.setDistrict(strings[2]);
        return  repository;
    }
    //查找对应rid下的iolog数量
    public Long findCount(Long rid){
        return inAndOutlogDAO.findCount(rid);
    }
    //逻辑删除对应rid的仓库
    public void deleteR(Long rid){
        Repository r = repositoryDAO.findOne(rid);
        r.setRepositoryIsDel("是");
        repositoryDAO.save(r);
    }
}
